package com.sainan114.pipelinestudy.strategy;

import com.sainan114.pipelinestudy.util.ExpressUtils;
import com.sainan114.pipelinestudy.util.LogUtils;
import com.sainan114.pipelinestudy.util.PipelineParamsMap;
import com.sainan114.pipelinestudy.util.StrategyKeyValueMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lgb
 * @Date: 2021/7/10
 * 这个类主要的作用就是，把加载到的数据进行判断
 */
@Component
public class JudgeStrategy extends AbstractStrategy {

    @Override
    public void baseProcess() {
        // 1. 先获取ql信息
        String strategyQLRule = PipelineParamsMap.getStrategyRule();
        // 2. 获取onloadStrategy信息
        List<String> onloadStrategy = StrategyKeyValueMap.getOnloadStrategy();
        if (onloadStrategy == null || onloadStrategy.size() < 2) {
            LogUtils.warnLog("date error onloadStrategy is empty or size < 2");
            throw new RuntimeException("date error");
        }
        String ChineseScale = onloadStrategy.get(0);
        String mathScale = onloadStrategy.get(1);
        String result = ExpressUtils.executeExpression(ChineseScale, mathScale, strategyQLRule);
        if (!StringUtils.isBlank(result)) {
            // 写入数据库...
            ArrayList<String> judgeResultList = new ArrayList<>();
            judgeResultList.add(result);
            StrategyKeyValueMap.setJudgeStrategy(judgeResultList);
        }

        LogUtils.infoLog("judgeStrategy is success");
    }

}
